package net.pm.core.entities.db;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import net.pm.common.toolkit.PMTool;
import net.pm.common.toolkit.date.DateUtils;
import net.pm.core.entities.PMCommandEntity;

import org.hibernate.annotations.GenericGenerator;


/**
 * The persistent class for the materiallog database table.
 * 物料出入记录
 */
@Entity
@Table(name="materiallog")
public class Materiallog extends PMCommandEntity<Materiallog>{
	/**
	 * 
	 */
	private static final long serialVersionUID = -211069639939829644L;
	private String id;
	private Integer getNumber;
	private Date operationTime;
	private Integer putNumber;
	private String remark;
	private Material material;
	private Collarmaterial collarmaterial;
	private Pmuser pmuser;
	private Short status;

	public static final Short status_put = Short.valueOf("0").shortValue();			//添加
	public static final Short status_get = Short.valueOf("1").shortValue();      //领取
	public static final Short status_return = Short.valueOf("2").shortValue();      //退料
    /**
     * 获取描述
     * @return
     */
    @Transient
    public String getDesc(){
    	StringBuilder builder = new StringBuilder();
    	builder.append(DateUtils.format(operationTime, DateUtils.FORMAT_LONG_CN)).append("\t");
    	builder.append(pmuser.getName());
    	if(PMTool.isNotEmpty(getNumber)){
    		builder.append("领取了").append(getNumber);
    	}else{
    		builder.append("添加了").append(putNumber);
    	}
    	
    	builder.append(material.getMaterialUnit());
    	builder.append("-").append(material.getMaterialName());
    	
    	return builder.toString();
    }
    
    public Materiallog() {
    }
    
	public Materiallog(String id) {
		super();
		this.id = id;
	}
	

	/**
	 * 新增时,退料时使用
	 * @param operationTime
	 * @param putNumber
	 * @param material
	 * @param pmuser
	 * @param status
	 */
	public Materiallog(Integer putNumber,
			Material material, Pmuser pmuser, Short status) {
		super();
		this.operationTime = DateUtils.obtainCurrentDate();
		this.putNumber = putNumber;
		this.material = material;
		this.pmuser = pmuser;
		this.status = status;
	}
	
	
	
	/**
	 * 领料时使用
	 * @param getNumber
	 * @param material
	 * @param collarmaterial
	 * @param pmuser
	 * @param status
	 */
	public Materiallog(Integer getNumber, 
			Material material, Collarmaterial collarmaterial, Pmuser pmuser,
			Short status) {
		super();
		this.getNumber = getNumber;
		this.operationTime = DateUtils.obtainCurrentDate();;
		this.material = material;
		this.collarmaterial = collarmaterial;
		this.pmuser = pmuser;
		this.status = status;
	}

	@GenericGenerator(name = "generator", strategy = "org.hibernate.id.UUIDGenerator")
	@Id
	@GeneratedValue(generator = "generator")
	@Column(unique=true, nullable=false, length=36)
	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}


	public Integer getGetNumber() {
		return this.getNumber;
	}

	public void setGetNumber(Integer getNumber) {
		this.getNumber = getNumber;
	}


    @Temporal( TemporalType.TIMESTAMP)
	@Column(nullable=false)
	public Date getOperationTime() {
		return this.operationTime;
	}

	public void setOperationTime(Date operationTime) {
		this.operationTime = operationTime;
	}


	public Integer getPutNumber() {
		return this.putNumber;
	}

	public void setPutNumber(Integer putNumber) {
		this.putNumber = putNumber;
	}


	@Column(length=200)
	public String getRemark() {
		return this.remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}



	//bi-directional many-to-one association to Material
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="MaterialId")
	public Material getMaterial() {
		return this.material;
	}

	public void setMaterial(Material material) {
		this.material = material;
	}
	

	//bi-directional many-to-one association to Collarmaterial
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="CollarMaterialId")
	public Collarmaterial getCollarmaterial() {
		return this.collarmaterial;
	}

	public void setCollarmaterial(Collarmaterial collarmaterial) {
		this.collarmaterial = collarmaterial;
	}
	

	//bi-directional many-to-one association to Pmuser
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="UserId")
	public Pmuser getPmuser() {
		return this.pmuser;
	}

	public void setPmuser(Pmuser pmuser) {
		this.pmuser = pmuser;
	}

	public Short getStatus() {
		return status;
	}

	public void setStatus(Short status) {
		this.status = status;
	}
	@Override
	public int compareTo(Materiallog o) {
		// TODO Auto-generated method stub
		return 0;
	}

	
}